package com.school.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.school.entity.HljtStudent;
import com.school.entity.HljtTranslate;
import com.school.service.HljtCheckWorkAttendanceService;
import com.school.util.JsonUtil;

@Controller
public class hljtCheckWorkAttendanceController {
	@Autowired
	private HljtCheckWorkAttendanceService hljtCheckWorkAttendanceService;
	
	/***
	 * 班级考勤 参数hljtStudent
	 * @param hljtStudent
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/classAttendance")
	@ResponseBody
	public JSONObject classAttendance(HljtStudent hljtStudent , JsonUtil jsonUtil) {
		try {
			if(null !=hljtStudent.getClassid()) {
				if(hljtStudent.getType().equals("0")) {
					hljtStudent.setCreatetime(this.getStartTime());
					List<HljtStudent>	temp = hljtCheckWorkAttendanceService.cardStudent(hljtStudent);
					List<HljtStudent>	noTemp = hljtCheckWorkAttendanceService.notCardStudent(hljtStudent);
					
					if(0 == temp.size() && 0 == noTemp.size()) {//已刷卡 ,未刷卡
						jsonUtil.setSchool("刷卡信息空");
					}else {
						Map<String , Object> map = new HashMap<>();
						map.put("studentsNumber", temp.size()+noTemp.size());
						map.put("studentsPayCard", temp);
						map.put("studentsPayCardNumber", temp.size());
						map.put("studentsNoPayCard", noTemp);
						jsonUtil.setSucceed(JSON.toJSONString(map) ,"刷卡信息");
					}
				}else if(hljtStudent.getType().equals("1")){//已刷卡

					hljtStudent.setCreatetime(this.getStartTime());
					List<HljtStudent>	temp = hljtCheckWorkAttendanceService.cardStudent(hljtStudent);
					
					if(0 == temp.size()) {
						jsonUtil.setSchool("已刷卡信息空");
					}else {
						Map<String , Object> map = new HashMap<>();
						map.put("studentsPayCard", temp);
						map.put("studentsPayCardNumber", temp.size());
						jsonUtil.setSucceed(JSON.toJSONString(map) ,"刷卡信息");
					}
				
					
				}else if(hljtStudent.getType().equals("2")){//未刷卡
					hljtStudent.setCreatetime(this.getStartTime());
					List<HljtStudent>	temp = hljtCheckWorkAttendanceService.notCardStudent(hljtStudent);
					
					if(0 == temp.size()) {
						jsonUtil.setSchool("未刷卡信息空");
					}else {
						Map<String , Object> map = new HashMap<>();
						map.put("studentsNoPayCard", temp);
						map.put("studentsNoPayCardNumber", temp.size());
						jsonUtil.setSucceed(JSON.toJSONString(map) ,"刷卡信息");
					}
				}else {
					jsonUtil.setFailMsg("type无效");
				}
			}else {
				jsonUtil.setnotClassId("classId空");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	 
	 /***
		 * 校园考勤 
		 * @param hljtStudent
		 * @param jsonUtil
		 * @return
		 */
		@RequestMapping("/campusAttendance")
		@ResponseBody
		public JSONObject campusAttendance(HljtStudent hljtStudent , JsonUtil jsonUtil) {
			try {
			
					hljtStudent.setCreatetime(this.getStartTime());
					List<HljtStudent>	temp = hljtCheckWorkAttendanceService.cardStudent(hljtStudent);
					List<HljtStudent>	noTemp = hljtCheckWorkAttendanceService.notCardStudent(hljtStudent);
					
					if(0 == temp.size() && 0 == noTemp.size()) {
						jsonUtil.setSchool("刷卡信息空");
					}else {
						Map<String , Object> map = new HashMap<>();
					//	map.put("studentsNumber", temp.size()+noTemp.size());
						map.put("studentsPayCard ", temp);
					//	map.put("studentsPayCardNumber ", temp.size());
						map.put("studentsNoPayCard ", noTemp);
					//	map.put("studentsNoPayCardNumber ", noTemp.size());
						jsonUtil.setSucceed(JSON.toJSONString(map) ,"刷卡信息");
					}
			
			} catch (Exception e) {
				e.printStackTrace();
				jsonUtil.setFailMsg("服务器异常");
			}
			return jsonUtil.toJSONString();
			
		}
		
		
		 private  Date getStartTime() {  
	         Calendar todayStart = Calendar.getInstance();  
	         todayStart.set(Calendar.HOUR_OF_DAY, 0);  
	         todayStart.set(Calendar.MINUTE, 0);  
	         todayStart.set(Calendar.SECOND, 0);  
	         todayStart.set(Calendar.MILLISECOND, 0);  
	         return todayStart.getTime();  
	     } 
	
	
		 
		 /***
			 * 添加考勤 参数hljtTranslate classid=1&attendanceModel=1&iccardid=4032432337
			 * @param hljtSchoolimage
			 * @param jsonUtil
			 * @return
			 */
			@RequestMapping("/addCheckWorkAttendance")
			@ResponseBody
			public JSONObject addCheckWorkAttendance(HljtTranslate hljtTranslate , JsonUtil jsonUtil) {
				try {
					if(null!=hljtTranslate &&null !=hljtTranslate.getClassid()&&null!=hljtTranslate.getAttendanceModel()) {
						Map<String, Object>	temp = hljtCheckWorkAttendanceService.addCheckWorkAttendance(hljtTranslate);
						if((boolean) temp.get("classid")) {
							if((boolean) temp.get("cardNotBoundStudent")) {
								//之前没有打卡
								if((boolean) temp.get("modeAttendance")) {
									if(null == (Integer)temp.get("row")) {
										jsonUtil.setRepeatCard("请勿重复打卡");
									}else {
										HljtStudent  s = (HljtStudent)temp.get("student");
										jsonUtil.setSucceed(JSON.toJSONString(s) ,"打卡信息");
									}
								}else {
									HljtStudent  s = (HljtStudent)temp.get("student");
									jsonUtil.setSucceed(JSON.toJSONString(s) ,"打卡信息");
								}
							}else {
								//之前已打卡
								jsonUtil.setSchool("该卡没有绑定学生");
							}
						}else {
							jsonUtil.setnotClassId("没有该classid");
						}
					}else {
						jsonUtil.setnotClassId("classid空");
					}
				} catch (Exception e) {
					e.printStackTrace();
					jsonUtil.setFailMsg("服务器异常");
				}
				return jsonUtil.toJSONString();
				
			}
}
